<template>
	<view class="form-add-record">
		<form @submit="formSubmit">
			<view class="uni-form-item uni-column">
				<view class="title">类型</view>
				<radio-group name="type">
					<label class="nowrap">
						<radio value="1" checked /><text>收</text>
					</label>
					<label class="nowrap">
						<radio value="-1" /><text>送</text>
					</label>
				</radio-group>
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">姓名</view>
				<input class="uni-input" name="name" placeholder="姓名" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">金额(元)</view>
				<input class="uni-input" type="number" name="price" placeholder="金额(元)" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">代理人</view>
				<input class="uni-input" name="agent" placeholder="如父亲代为还礼" />
			</view>
			<view class="uni-btn-v">
				<button form-type="submit">确定</button>
			</view>
		</form>
	</view>
</template>
<script>
import Bmob from 'hydrogen-js-sdk'
import { mapMutations } from 'vuex'

export default {
	name: "AddRecord",
	props: {
		name: String,
	},
	data() {
		return {

		}
	},
	computed: {

	},
	methods: {
		...mapMutations(['navigateBack']),
		async formSubmit(e) {
			var v = e.target.value;
			if (!v.name) return this.$toast.error('姓名必须填')
			if (!v.price) return this.$toast.error('金额必须填')
			var price = (v.type == '1' ? 1 : -1) * v.price
			var record = Bmob.Query('record')
			record.set('name', v.name)
			record.set('price', price)
			if (v.agent) record.set('agent', v.agent)
			await record.save()
			this.navigateBack(record)
		},
		formReset(e) {
			console.log(e)
		},
	},
	mounted() {

	},
	components: {

	},
}
</script>
<style>
.form-add-record input {
	padding: 12px;
}
</style>
